package com.inspectortime.webapp;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.inspectortime.domain.Inspector;
import com.inspectortime.domain.validate.InspectorCompanyValidator;
import com.inspectortime.domain.validate.UserValidator;
import com.mkode.util.java.lang.ClassUtils;

public class RegisterValidator implements Validator {
	
	private UserValidator userValidator;
	public void setUserValidator(UserValidator userValidator) {
		this.userValidator = userValidator;
	}

	private InspectorCompanyValidator inspectorCompanyValidator;

	public void setInspectorCompanyValidator(
			InspectorCompanyValidator inspectorCompanyValidator) {
		this.inspectorCompanyValidator = inspectorCompanyValidator;
	}

	public void setEmployeeValidator(UserValidator userValidator) {
		this.userValidator = userValidator;
	}

	public boolean supports(Class clazz) {
		return ClassUtils.isInstanceOf(clazz, Inspector.class);
	}

	public void validate(Object target, Errors errors) {
		Inspector inspector = (Inspector) target;
		userValidator.validate(inspector, errors);
		errors.pushNestedPath("company");
		inspectorCompanyValidator.validate(inspector.getCompany(), errors);
		inspectorCompanyValidator.validateForDuplicates(inspector.getCompany(), errors);
		errors.popNestedPath();
	}

}
